package edu.whu.rose.clock.ssql.util;

public class SequentialStrArray {

	private String[] data;
	private int size = 10;
	private int capacity = 0;
	
	public SequentialStrArray() {
		data = new String[size];
	}
	
	public int binarySearch(String target) {
		int low = 0;
		int mid = 0;
		int top = capacity - 1;
		
		while (low <= top) {
			mid = (low + top) / 2;
			if (target.compareTo(data[mid]) < 0) {
				top = mid - 1;
			}
			else if (target.compareTo(data[mid]) > 0) {
				low = mid + 1;
			}
			else if (target.compareTo(data[mid]) == 0) {
				return mid;
			}
		}
		
		return -1;
	}
	
	public void insert(String str) {
		if (capacity == size) {
			size = 2 * size;
			String[] temp = data;
			data = new String[size];
			for (int i = 0; i < temp.length; i++) {
				data[i] = temp[i];
			}
		}
		
		int low = 0;
		int mid = 0;
		int top = capacity - 1;
		
		while (low <= top) {
			mid = (low + top) / 2;
			if (str.compareTo(data[mid]) < 0) {
				top = mid - 1;
			}
			else if (str.compareTo(data[mid]) > 0) {
				low = mid + 1;
			}
			else if (str.compareTo(data[mid]) == 0) {
				return;
			}
		}
		
		for (int i = capacity; i > low; i--) {
			data[i] = data[i - 1];
		}
		data[low] = str;
		
		capacity++;
	}
	
	public int getCapacity() {
		return capacity;
	}
	
	public String get(int index) {
		return data[index];
	}

}
